<?php

class BackendController extends Controller
{
	
	public $layout='//layouts/column2';
	
	public function filters()
	{
		return array(
				'accessControl', // perform access control for CRUD operations
				'postOnly + delete', // we only allow deletion via POST request
		);
	}
	
	
	public function accessRules()
	{
		return array(
				array('allow',  // allow all users to perform 'index' and 'view' actions
						'actions'=>array('index','view'),
						'users'=>array('*'),
				),
				array('allow', // allow authenticated user to perform 'create' and 'update' actions
						'actions'=>array('addshoe','viewshoe','updateshoe','delshoe','shoecatalog',
								'categorie','addcategorie','updatecategorie','delcategorie',
								'customergroup','addcustomergroup','ucustomergroup','dcustomergroup',
								'customer','addcustomer','delcustomer','updatecustomer','comment',
								'setting','page', 'addpage','updatepage'),
						'users'=>array('@'),
				),
				array('allow', // allow admin user to perform 'admin' and 'delete' actions
						'actions'=>array('admin','delete'),
						'users'=>array('vmgr'),
				),
				array('deny',  // deny all users
						'users'=>array('*'),
				),
		);
	}
	
	public function actionAddshoe()
	{
		$publicpath =  "/public/uploads/" . date ( 'Y' ) . '/' . date ( 'm' ) . '/';
		$uploadpath = Yii::app() -> getBasePath() . "/.." . $publicpath;
		if (! is_dir ( $uploadpath ))
			mkdir ( $uploadpath, 0777, 1 );
		
		$model=new Shoe;
		
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
		
	
		if(isset($_POST['Shoe']))
		{
			$publicpath =  "/public/uploads/" . date ( 'Y' ) . '/' . date ( 'm' ) . '/';
			$uploadpath = Yii::app() -> getBasePath() . "/.." . $publicpath;
			
			$file_content = CUploadedFile::getInstance ( $model, 'shoe_content' );
			$content_newName = substr ( md5 ( $file_content->extensionName . round ( (microtime ( true ) * 1000) ) ), 0, 17 ) . '.' . $file_content->extensionName;
			$content_filename = $uploadpath . $content_newName;
			$file_content->saveAs($content_filename, TRUE);
			$_POST['Shoe']['shoe_content'] = $publicpath  . $content_newName ;
		
			
			$file_cover = CUploadedFile::getInstance ( $model, 'item_cover' );
			if (isset($file_cover))
			{
				$cover_newName = substr ( md5 ( $file_cover->extensionName . round ( (microtime ( true ) * 1000) ) ), 0, 17 ) . '.' . $file_cover->extensionName;
				$cover_filename = $uploadpath . $cover_newName;
					
				$file_cover->saveAs ( $cover_filename, TRUE );
			
				$_POST['Shoe']['item_cover'] =  $publicpath  . $cover_newName ;
			
			} else {
				$_POST['Shoe']['item_cover'] = $cover;
							
			}
				
			$model->attributes=$_POST['Shoe'];
			if($model->save())
				$this->redirect(array('/backend/shoecatalog'));
		}
		
		$this->render('/shoe/create',array(
				'model'=>$model,
		));
	}
	
	public function actionViewshoe($id)
	{
		$model=$this->loadShoe($id);

		$this->render('/shoe/view',array(
				'model'=>$model,
		));
	}
	
	public function actionUpdateshoe($id)
	{
		$model=$this->loadShoe($id);
	
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
		
		$cover = $model->attributes['item_cover'];
		$content = $model->attributes['shoe_content'];
		
		
		if(isset($_POST['Shoe']))
		{
			$publicpath =  "/public/uploads/" . date ( 'Y' ) . '/' . date ( 'm' ) . '/';
			$uploadpath = Yii::app() -> getBasePath() . "/.." . $publicpath;
			
			$file_content = CUploadedFile::getInstance ( $model, 'shoe_content' );
			if (isset($file_content))
			{
				$content_newName = substr ( md5 ( $file_content->extensionName . round ( (microtime ( true ) * 1000) ) ), 0, 17 ) . '.' . $file_content->extensionName;
				$content_filename = $uploadpath . $content_newName;
				$file_content->saveAs($content_filename, TRUE);
				$_POST['Shoe']['shoe_content'] = $publicpath  . $content_newName ;
			} else 	{
				$_POST['Shoe']['shoe_content'] = $content;
			}
			
			$file_cover = CUploadedFile::getInstance ( $model, 'item_cover' );
			if (isset($file_cover))
			{
				$cover_newName = substr ( md5 ( $file_cover->extensionName . round ( (microtime ( true ) * 1000) ) ), 0, 17 ) . '.' . $file_cover->extensionName;
				$cover_filename = $uploadpath . $cover_newName;
					
				$file_cover->saveAs ( $cover_filename, TRUE );
			
				$_POST['Shoe']['item_cover'] =  $publicpath  . $cover_newName ;
			
			} else {
				$_POST['Shoe']['item_cover'] = $cover;
							
			}
			
			$model->attributes=$_POST['Shoe'];
			if($model->save())
				$this->redirect(array('/backend/shoecatalog','id'=>$model->ID));
			}
	
		$this->render('/shoe/update',array(
				'model'=>$model,
		));
	}
	
	public function actionDelshoe($id)
	{
		
		$this->loadShoe($id)->delete();
	
		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}
	
	public function actionShoecatalog()
	{
		$model=new Shoe('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Shoe']))
			$model->attributes=$_GET['Shoe'];
		
		$this->render('/shoe/admin',array(
				'model'=>$model,
		));
	}
	
	
	public function loadShoe($id){
		$model=Shoe::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
	
	
	public function actionCategorie()
	{
		$model=new Categories('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Categories']))
			$model->attributes=$_GET['Categories'];

	
		$this->render('/categorie/admin',array(
				'model'=>$model,
		));
	}
	
	public function actionAddcategorie()
	{
		$model=new Categories;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Categories']))
		{
			$publicpath =  "/public/uploads/" . date ( 'Y' ) . '/' . date ( 'm' ) . '/';
			$uploadpath = Yii::app() -> getBasePath() . "/.." . $publicpath;
			if (! is_dir ( $uploadpath ))
				mkdir ( $uploadpath, 0777, 1 );
			
			$file = CUploadedFile::getInstance ( $model, 'category_image_url' );
			if (isset($file)){
				$newName = substr ( md5 ( $file->extensionName . round ( (microtime ( true ) * 1000) ) ), 0, 17 ) . '.' . $file->extensionName;
				$file_name = $uploadpath . $newName;
				
				
				$file->saveAs ( $file_name, TRUE );
				
				$_POST['Categories']['category_image_url'] = Yii::app()->request->hostInfo . $publicpath  . $newName ;
			}
			
			$model->attributes=$_POST['Categories'];
			if($model->save() )
				//var_dump($_POST['Categories']);
				$this->redirect(array('/backend/categorie','id'=>$model->id));
		}

		$this->render('/categorie/create',array(
			'model'=>$model,
		));
	}
	
	public function actionUpdatecategorie($id)
	{
		$model=$this->loadCategorie($id);
	
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
	
		if(isset($_POST['Categories']))
		{
			$publicpath =  "/public/uploads/" . date ( 'Y' ) . '/' . date ( 'm' ) . '/';
			$uploadpath = Yii::app() -> getBasePath() . "/.." . $publicpath;
			if (! is_dir ( $uploadpath ))
					mkdir ( $uploadpath, 0777, 1 );
					
				$file = CUploadedFile::getInstance ( $model, 'category_image_url' );
				
				if (isset($file)){
					$newName = substr ( md5 ( $file->extensionName . round ( (microtime ( true ) * 1000) ) ), 0, 17 ) . '.' . $file->extensionName;
					$file_name = $uploadpath . $newName;
	
					$file->saveAs ( $file_name, TRUE );
					$_POST['Categories']['category_image_url'] = Yii::app()->request->hostInfo . $publicpath  . $newName ;
				}
				
				$model->attributes=$_POST['Categories'];
				if($model->save())
					//var_dump($_POST['Categories']);
					$this->redirect(array('/backend/categorie/'));
		}
	
		$this->render('/categorie/update',array(
				'model'=>$model,
		));
	}
	
	

	public function actionDelcategorie($id)
	{
		$this->loadCategorie($id)->delete();
	
		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('delcategorie'));
	}
	
	public function loadCategorie($id)
	{
		$model=Categories::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
	
	
	
	public function actionCustomergroup()
	{
		$model=new Customergroup('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Customergroup']))
			$model->attributes=$_GET['Customergroup'];
	
		$this->render('/customergroup/admin',array(
				'model'=>$model,
		));
	}
	
	
	public function actionAddcustomergroup()
	{
		$model=new Customergroup;
	
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
	
		if(isset($_POST['Customergroup']))
		{
			$model->attributes=$_POST['Customergroup'];
			if($model->save())
				$this->redirect(array('/backend/customergroup'));
		}
	
		$this->render('/customergroup/create',array(
				'model'=>$model,
		));
	}
	
	
	public function actionUcustomergroup($id)
	{
		$model=$this->loadCustomergroup($id);
	
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
	
		if(isset($_POST['Customergroup']))
		{
			$model->attributes=$_POST['Customergroup'];
			if($model->save())
				$this->redirect(array('/backend/customergroup'));
		}
	
		$this->render('/customergroup/update',array(
				'model'=>$model,
		));
	}
	
	
	public function actionDcustomergroup($id)
	{
		$this->loadCustomergroup($id)->delete();
	
		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('/backend/customergroup'));
	}
	
	public function loadCustomergroup($id)
	{
		$model=Customergroup::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
	
	
	public function actionCustomer()
	{
		$model=new Customer('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Customer']))
			$model->attributes=$_GET['Customer'];

		$this->render('/customer/admin',array(
			'model'=>$model,
		));
	}
	
	public function actionAddcustomer()
	{
		$model=new Customer;
	
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
	
		if(isset($_POST['Customer']))
		{			
			$_POST['Customer']['customer_pass'] = md5($_POST['Customer']['customer_pass']);
			$model->attributes=$_POST['Customer'];
			if($model->save())
				$this->redirect(array('/backend/customer'));
		}
	
		$this->render('/customer/create',array(
				'model'=>$model,
		));
	}
	
	public function actionDelcustomer($id)
	{
		$this->loadCustomer($id)->delete();
	
		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('/backend/customer'));
	}
	
	
	public function actionUpdatecustomer($id)
	{
		$model=$this->loadCustomer($id);
		
	
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
	
		if(isset($_POST['Customer']))
		{
			if ($model->attributes['customer_pass'] == $_POST['Customer']['customer_pass']){
					$model->attributes=$_POST['Customer'];
			} else {
				$_POST['Customer']['customer_pass'] = md5($_POST['Customer']['customer_pass']);
				$model->attributes=$_POST['Customer'];
			}
			
			if($model->save())
				$this->redirect(array('/backend/customer'));
		}
	
		$this->render('/customer/update',array(
				'model'=>$model,
		));
	}
	
	
	public function loadCustomer($id)
	{
		$model=Customer::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
	
	public function actionSetting()
	{
		
		$model=$this->loadUser(Yii::app()->user->ID);
	
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
	
		if(isset($_POST['User']))
		{
			
			if ($model->attributes['user_pass'] == $_POST['User']['user_pass']){
				$model->attributes=$_POST['User'];
			} else {
				$_POST['User']['user_pass'] = md5($_POST['User']['user_pass']);
				$model->attributes=$_POST['User'];
			}
			
			if($model->save())
				$this->redirect(array('/backend/setting'));
		}
	
		$this->render('/user/update',array(
				'model'=>$model,
		));
	}
	
	public function loadUser($id)
	{
		$model=User::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
	
	
	public function actionComment()
	{
		$model=new Comment('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Comment']))
			$model->attributes=$_GET['Comment'];
	
		$this->render('/comment/admin',array(
				'model'=>$model,
		));
	}
	
	
	public function actionPage()
	{
		$model=new Page('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Page']))
			$model->attributes=$_GET['Page'];
	
		$this->render('/page/admin',array(
				'model'=>$model,
		));
	}
	
	
	public function actionAddpage()
	{
		$model=new Page;
	
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
	
		if(isset($_POST['Page']))
		{
			$model->attributes=$_POST['Page'];
			if($model->save())
				$this->redirect(array('/backend/page'));
		}
	
		$this->render('/page/create',array(
				'model'=>$model,
		));
	}
	
	public function actionUpdatepage($id)
	{
		$model=$this->loadPage($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Page']))
		{
			$model->attributes=$_POST['Page'];
			if($model->save())
				$this->redirect(array('/backend/page'));
		}

		$this->render('/page/update',array(
			'model'=>$model,
		));
	}
	
	
	public function loadPage($id)
	{
		$model=Page::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
	
	
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}